<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>

  <h2>计算代码</h2>
  <pre>
    <code>{{compute_code}}</code>
  </pre>

  <h2>计算结果</h2>
  <pre>
    <code>{{computed_value}}</code>
  </pre>

  <h2>模型项目</h2>
  <pre>
    <code>{{object}}</code>
  </pre>
</template>

<script>
import {_config} from "@/js/s94_tool.js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this;
    let model_name = this.$get('model_name', ""), btns = [
      {title:'重置', type:'', onclick(){ app.$refs["forms"].init(); }},
      {title:'测试计算代码', type:'primary', onclick(){ app.$refs["forms"].submit(); }},
    ];

    let model_map = _config('model_map', {});
    let model_name_config = {};
    Object.values(model_map).forEach((v)=>{
      model_name_config[v.name] = v.label;
    })

    let forms_config = {
      model_name: {label: '数据模型', name: 'model_name', type:'select_one',config:model_name_config, tips: '', value:model_name},
      link_id: {label: '数据ID', name: 'link_id', type:'text_one',config:'number', tips: '', value:0},

      code: {label: '代码模板', name: 'code', type:'select_one',config:[], tips: '', value:''},
      code_input: {label: '自定义代码', name: 'code_input', type: 'text_more', tips: '编写代码命令，参考文档：<a href="/sys/index/help?name=compute_code" target="_blank">《算计代码》</a>', value:""},
    }

    return {
      btns: btns,
      config: forms_config,

      object: null,
      computed_value: null,

      code_config_base: [
        {content:'自定义代码', value:''},
        {content:'关联用户', value:'', children: [
            {content:'用户id', value:'get($this->info,"user_id")'},
            {content:'昵称', value:'get($this->info,"user.nickname")'},
            {content:'电话', value:'get($this->info,"user.phone")'},
            {content:'性别', value:'get(["未知","男","女"],get($this->info,"user.sex"))'},
            {content:'等级', value:'get(model_find("user_level",["level","=",get($this->info,"user.level")]),"name")'},
            {content:'余额', value:'user_money(get($this->info,"user_id"))'},
          ]},
        {content:'当前日期', value:'', children: [
            {content:'年-月-日 时:分:秒', value:'date("Y-m-d H:i:s")'},
            {content:'年-月-日 时:分', value:'date("Y-m-d H:i")'},
            {content:'年-月-日', value:'date("Y-m-d")'},
            {content:'年-月', value:'date("Y-m")'},
          ]},
      ],
      code_config_extend: {
        user_money_recharge: {content:'用户充值', value:'', children: [
            {content:'支付金额', value:'get($this->info,"pay_money")'},
            {content:'赠送金额', value:'get($this->info,"give_money")'},
            {content:'充值前余额', value:'get($this->info,"has_money")'},
            {content:'支付单号', value:'get($this->info,"log_pay.pay_no")'},
            {content:'退回方式(原路退回、线下退款)', value:'get(["pay_type"=>"原路退回","offline"=>"线下退款"],get($this->info,"log_refund.refund_type"))'},
            {content:'充值时间', value:'get($this->info,"log_pay.pay_time")'},
          ]}
      }
    }
  },
  methods: {
    onsubmit(formData){
      let app = this;
      ajax({url:'/admin/api/test_compute_code', method:'POST', data:formData, skip_error_handle:true}, (res)=>{
        app.object = res.data.object;
        app.computed_value = res.data.computed_value;
      })
    },
    get_code_config(){
      let model_name = this.config.model_name.value;
      let res = this.code_config_base;
      if (model_name){
        let code_config_add = this.code_config_extend[model_name] || [];
        res = res.concat(code_config_add);
      }else {
        Object.values(this.code_config_extend).forEach((row)=>{
          res = res.concat(row);
        })
      }
      return res;
    }
  },
  watch: {
    'config.code.value'(v){
      this.config.code_input.remove = !!v;
    },
    'config.model_name.value'(v){
      this.config.code.config = this.get_code_config();
    },
  },
  computed: {
    compute_code(){
      if (this.config.code.value) return this.config.code.value;
      return this.config.code_input.value || "";
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    this.config.code.config = this.get_code_config();

  }
}
</script>
